﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;

namespace LPV_POS.mh_pos
{
    public partial class mh_dmtiente : DevExpress.XtraEditors.XtraForm
    {
        thuvien tv;
        DataTable tb;
        string[] s;
        object[] o;
        // select danh sach tien te
        private DataTable f_selecttiente()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmtiente", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

         // Tim tien te khi nhan nut tim
         private DataTable f_selectfindtientu(string tentiente, string ghichu)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@GHICHU"};
            o = new object[] { 1, 2, tentiente, ghichu };
            tb = tv.SP_SelectDataTable("sp_mh_dmtiente", s, o);
            return tb;
        }
        // select danh sach tien te co dieu kien
        private DataTable f_findtiente()
        {
              DataTable tb;
            string tentiente;
            string ghichu;

              if (txt_tentiente.Text != "")
                tentiente= "'%" + txt_tentiente.Text + "%'";
            else
                tentiente= "";

              if (txt_ghichu.Text != "")
                ghichu= "'%" + txt_ghichu.Text + "%'";
            else
                ghichu= "";

             tb = f_selectfindtientu(tentiente,ghichu);
            return tb;
        }

        // insert them tien te
        private int f_inserttiente(string tentiente, string ghichu)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME","@GHICHU" };
            o = new object[] { 2, 1, tentiente, ghichu };
            int ketqua = tv.SP_Update("sp_mh_dmtiente", s, o);
            return ketqua;
        }

        // update mot tien te
        private int f_updatetiente(string matiente, string tentiente, string ghichu)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME","@GHICHU" };
            o = new object[] { 3, 1, matiente, tentiente, ghichu };
            int ketqua = tv.SP_Update("sp_mh_dmtiente", s, o);
            return ketqua;
        }

        // xoa tien te
        private int f_deletetiente(string matiente)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, matiente };
            int ketqua = tv.SP_Update("sp_mh_dmtiente", s, o);
            return ketqua;
        }

        public mh_dmtiente()
        {
            InitializeComponent();
        }

        private void mh_tiênte_Load(object sender, EventArgs e)
        {
            luoi_tiente.MainView = grid_tiente;
            luoi_tiente.DataSource = f_selecttiente();
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_tentiente.Text = "";
            txt_ghichu.Text = "";
            bton_luu.Enabled = true;
            bton_tentiente.Enabled = true;
            bton_ghichu.Enabled = true;
            Variable.str_trangthaithemsua = "them";
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            bton_luu.Enabled = true;
            bton_tentiente.Enabled = true;
            bton_ghichu.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (luoi_tiente.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string matiente = grid_tiente.GetRowCellValue(grid_tiente.FocusedRowHandle, col_makhuvuc).ToString();
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletetiente(matiente);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công, kiểm tra lai khoa ngoại", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_tiente.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_tiente.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            int ketqua;

            if (txt_tentiente.Text == "")       // không thể luu neu chua nhap ten don vi tinh
                return;
            int chiso = -1;
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_inserttiente(txt_tentiente.Text,txt_ghichu.Text);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                chiso = ((ColumnView)luoi_tiente.Views[0]).FocusedRowHandle;
                string matiente = grid_tiente.GetRowCellValue(grid_tiente.FocusedRowHandle, col_makhuvuc).ToString();
                ketqua = f_updatetiente(matiente, txt_tentiente.Text,txt_ghichu.Text);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // luoi_nhomchuhh.DataSource = f_selectdsnhomchuhh();
            }
            luoi_tiente.DataSource = null;
            luoi_tiente.DataSource = f_selecttiente();
            if (Variable.str_trangthaithemsua == "sua" && chiso != -1)
            {
                grid_tiente.SelectRow(chiso);
                grid_tiente.FocusedRowHandle = chiso;

            }
        }

        private void grid_tiente_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            //   str_makho = grid.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_tiente.FocusedColumn;
            txt_tentiente.Text = grid_tiente.GetRowCellDisplayText(chiso, col_tenkhuvuc) + "";
            txt_ghichu.Text = grid_tiente.GetRowCellDisplayText(chiso, col_ghichu) + "";
            bton_luu.Enabled = false;
            bton_tentiente.Enabled = false;
            bton_ghichu.Enabled = false;
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
             Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {

                this.Close();
            }
        }

        private void bton_clr_Click(object sender, EventArgs e)
        {
            txt_tentiente.Text = "";
            txt_ghichu.Text = "";
            bton_tentiente.Enabled = true;
            bton_ghichu.Enabled = true;
        }

        private void bton_tim_Click(object sender, EventArgs e)
        {
           
             luoi_tiente.DataSource = null;
             luoi_tiente.DataSource = f_findtiente();
             
        }

        private void bton_tentiente_Click(object sender, EventArgs e)
        {
            //txt_tentiente.Text = cpublic.loadformchu("mh_dmtiente");
            txt_tentiente.Text = new cpublic().loadformchu_point(300,200);
        }

        private void bton_ghichu_Click(object sender, EventArgs e)
        {
            //txt_tentiente.Text = cpublic.loadformchu("mh_dmtiente");
            txt_tentiente.Text = new cpublic().loadformchu_point(300, 200);
        }
        }
    }
